<!-- 防治管理 -->
<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList(),searchPage()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">鱼塘名称</div>
            <el-select filterable clearable v-model="searchObj.fishPondId" >
              <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName" :value="item.fishPondId" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">药品名称</div>
            <el-select filterable clearable v-model="searchObj.materialId">
              <el-option v-for="(item, index) in materialList" :key="index" :label="item.name" :value="item.materialId" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">操作人</div>
            <el-select filterable clearable v-model="searchObj.staffId">
              <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName"
                :value="item.staffId" />
            </el-select>
          </div>
          <div class="select-box-content  select-box-leng-content">
            <div class="select-box-content-name">操作日期</div>
            <el-date-picker v-model="operationTime" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" clearable />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="162" @add="add" @del="del" />
    </template>
    <template v-slot:categoryBox>
      <CategoryBar :categoryList="categoryList" @categoryBar="categoryBar" />
    </template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="
          row => {
            return row.epidemicPreventionId;
          }
        " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column v-if="searchObj.auditType === 'REVIEW'" type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column key="index" type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.epidemicPreventionId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column  key="typeStr" prop="typeStr" label="防治/预防" >
        <template slot-scope="scope" v-if="scope.row.epidemicPreventionId">
           <span v-if="scope.row.type=='EpidemicPrevention'">防治疾病</span>
           <span v-if="scope.row.type=='DisinfectWork'">日常预防</span>
          </template>
        </el-table-column>
        <el-table-column  key="diseaseName" prop="diseaseName" label="防治疫病名称" >
          <template slot-scope="scope" v-if="scope.row.epidemicPreventionId">
            {{ scope.row.diseaseName||'--' }}
          </template>
        </el-table-column>
        <el-table-column  key="pondName" prop="pondName" label="防治鱼塘名称" />
        <el-table-column  key="batchName" prop="batchName" label="批次编号" />
        <el-table-column  key="num" label="药品与数量">
          <template slot-scope="scope" v-if="scope.row.epidemicPreventionId">
            <el-button class="text-btn-cursor" type="text" size="small" @click="showDetail(scope.row)">点击查看
            </el-button>
          </template>
        </el-table-column>
        <el-table-column key="staffName" prop="staffName" label="操作人" />
        <el-table-column key="operationTime" prop="operationTime" label="操作日期" />
        <el-table-column key="operation" v-if="searchObj.auditType !== 'PASS'" label="操作" width="250">
          <template v-if="scoped.row.epidemicPreventionId" slot-scope="scoped">
            <div class="table-btns-box">
              <div v-show="searchObj.auditType === 'REVIEW'" class="table-btns edit-btns" @click="edit(scoped.row)"
                v-if="judgePermission(162, 'edit')">
                <p>修改</p>
              </div>
              <div v-show="searchObj.auditType === 'REVIEW'" class="table-btns del-btns" @click="delOne(scoped.row)"
                v-if="judgePermission(162, 'deleteone')">
                <p>删除</p>
              </div>
              <div v-show="searchObj.auditType === 'NOT_PASS'" class="table-btns edit-btns"
                @click="showUnpassReason(scoped.row)">
                <p>未通过原因</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="'未通过原因'" :visible.sync="showDialog" width="25%" top="0" :close-on-click-modal="false"
        @close="cancel">
        <el-form label-position="left" class="el-form">
          <el-form-item label="不通过原因: " prop="failReason" class="w100p">
            <el-input type="textarea" :rows="3" disabled v-model="editObject.failReason">
            </el-input>
          </el-form-item>
        </el-form>
      </el-dialog>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :staffList="staffList" :show.sync="showAddEditDialog" @closeDialog="closeDialog"
        @cancelDialog="cancel" />
      <CompleteInfoDialog ref="completeInfoDialog" :now-chance-stau="nowChanceStau"
        :complete-info-object="completeInfoObject" :show.sync="showCompleteInfoDialog" @closeDialog="closeDialog"
        @cancelDialog="cancel" />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import CategoryBar from "@/components/CategoryBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import CompleteInfoDialog from "./components/complete-drug-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import EpidemicPreventionManagementApi from "@/api/health-management/epidemic-prevention-management";
import BatchApi from "@/api/batch-mange/index";
import MaterialApi from "@/api/basic-info-manage/material";
import FishPondApi from "@/api/basic-info-manage/fishPond";
import EnumApi from "@/api/enum";
import StaffApi from "@/api/basic-info-manage/staff";

export default {
  name: "EpidemicPreventionManagement",
  components: { ActionBar, CategoryBar, Pagination, BaseMainSlot, AddEditDialog, CompleteInfoDialog },
  mixins: [mymixin],
  data () {
    return {
      // 查询条件：
      searchObj: {
        auditType: undefined, // 审核状态
        staffId: undefined, // 员工名称
        materialId: undefined, // 药品ID
        fishPondId: undefined, // 鱼塘ID
      },
      operationTime: undefined, // 操作时间
      batchList: undefined,
      materialList: undefined,
      pondList: undefined,
      checkedIdList: [],
      categoryList: [],
      staffList: [],
      showAddEditDialog: false,
      completeInfoObject: {},
      showCompleteInfoDialog: false,
      showDialog: false,
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    // this.queryPageList();
    this.queryBatch()
    this.queryMaterial()
    this.queryFishPond()
    this.queryEnum()
    this.queryStaff()
  },
  mounted () { },
  methods: {
    searchPage(){
      this.currentPage=1
      this.queryPageList();
      setTimeout(() => {
        this.queryPageList();
      }, 200);
    },
    queryStaff () {
      StaffApi.queryByCondition().then(res => {
        this.staffList = res.data
      })
    },
    categoryBar (item) {
      this.currentPage = 1;
      if (item.value) this.searchObj.auditType = item.value;
      this.queryPageList();
    },
    queryEnum () {
      EnumApi.list({ enumType: "AuditType" }).then(res => {
        this.categoryList = []
        res.data.forEach((item, index) => {
          this.categoryList.push({
            id: index,
            name: item.value,
            value: item.name
          })
        });
      })
    },
    queryBatch () {
      BatchApi.queryByCondition({}).then(res => {
        this.batchList = res.data
      })
    },
    queryMaterial () {
      MaterialApi.queryByCondition({ materialTypeNo: 'drug' }).then(res => {
        this.materialList = res.data;
      })
    },
    queryFishPond () {
      FishPondApi.queryByCondition({}).then(res => {
        this.pondList = res.data;
      })
    },
    showDetail (row) {
      if (row.epidemicPreventionId) {
        this.completeInfoObject = JSON.parse(JSON.stringify(row));
        this.showCompleteInfoDialog = true
      }
    },
    // 展示查看未通过原因弹窗
    showUnpassReason (row) {
      this.nowChanceStau = 'failReason'
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showDialog = true
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: { ...this.searchObj }
      };
      if (this.operationTime) {
        params.entity.operationStartTime = this.operationTime[0];
        params.entity.operationEndTime = this.operationTime[1].replace('00:00:00', '24:00:00');
      }
      EpidemicPreventionManagementApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 单个删除
    delOne (data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        EpidemicPreventionManagementApi.delById({ id: data.epidemicPreventionId }).then(res => {
          if (res.success) {
            this.resetActionBar()
            this.$message.success("删除成功");
            this.queryPageList();
            this.resetActionBar();
          }
        });
      })
        .catch(() => {
          this.resetActionBar()
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage () {
      EpidemicPreventionManagementApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    changePageSize (pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.epidemicPreventionId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.epidemicPreventionId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.epidemicPreventionId) {
          return item.epidemicPreventionId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 添加
    add () {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.showAddEditDialog = true;
    },
    // 修改
    edit (row) {
      if (row.epidemicPreventionId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
    // 取消
    cancel () {
      this.nowChanceStau = '';
      this.titleText = ''
      this.editObject = {}
      this.addActive = true;
      this.delActive = false;
      this.showAddEditDialog = false
      this.showCompleteInfoDialog = false;
      this.showDialog = false;
      this.resetActionBar()
    },
  }
};
</script>

<style lang="scss" scoped></style>
